package com.carrot.api.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.carrot.common.annotation.Log;
import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.SysPointDto;
import com.carrot.common.core.domain.entity.SysPoint;
import com.carrot.common.core.domain.entity.SysStation;
import com.carrot.common.core.domain.vo.SysPointVo;
import com.carrot.common.enums.BusinessType;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.mybatis.Page;
import com.carrot.common.utils.StringUtils;
import com.carrot.common.utils.WrapperUtil;
import com.carrot.common.utils.poi.ExcelUtil;
import com.carrot.system.service.ISysPointService;
import com.carrot.system.service.ISysStationService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 点表工具
 *
 * @author liuhongzhen
 */
@RestController
@RequestMapping("/system/point")
public class SysPointController extends BaseController {

    @Autowired
    private ISysPointService pointService;
    @Autowired
    private ISysStationService stationService;

    /**
     * 场站列表
     */
    @GetMapping("stationList")
    public AjaxResult stationList() {

        List<SysStation> list = stationService.list();

        return AjaxResult.success(list);
    }

    @PostMapping("/list")
    public AjaxResult list(@Validated @RequestBody SysPointDto dto) {

        Page<SysPoint, SysPointVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<SysPoint> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(dto.getSearchContent())) {
            queryWrapper.like("station_id", dto.getSearchContent())
                    .or()
                    .like("info_obj_addr", dto.getSearchContent())
                    .or()
                    .like("equipment_id", dto.getSearchContent())
                    .or()
                    .like("description", dto.getSearchContent())
                    .or()
                    .like("standard_description", dto.getSearchContent());
        }
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getStationId()), "station_id", dto.getStationId());
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getEquipmentType()), "equipment_type", dto.getEquipmentType());
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getMatchType()), "match_type", dto.getMatchType());
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getOnlineEnable()), "online_enable", dto.getOnlineEnable());
        queryWrapper.orderByAsc("create_time");
        page.setQueryWrapper(queryWrapper);

        page = pointService.findPage(page);

        List<SysPoint> tList = page.getTList();
        List<SysPointVo> vList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(tList)){
            Set<String> stationIds = tList.stream().map(SysPoint::getStationId).collect(Collectors.toSet());
            List<SysStation> stationList = stationService.query().in("station_id",stationIds).list();
            Map<String,String> stationMap = stationList.stream().collect(Collectors.toMap(SysStation::getStationId,SysStation::getStationName));

            tList.forEach(point ->{
                SysPointVo pointVo = WrapperUtil.transform(point,SysPointVo.class);
                pointVo.setStationName(stationMap.getOrDefault(point.getStationId(),""));
                vList.add(pointVo);
            });
        }
        page.setVList(vList);
        return AjaxResult.success(page.getPageDataVo2());
    }

    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("id") Long id) {

        SysPoint sysPoint = pointService.getById(id);
        return AjaxResult.success(sysPoint);
    }

    @GetMapping("/del")
    public AjaxResult del(@RequestParam("id") Long id) {
        pointService.del(id);
        return AjaxResult.success();
    }

    @PostMapping("/batchDel")
    public AjaxResult batchDel(@RequestBody SysPointDto dto) {
        if (CollUtil.isEmpty(dto.getIds())) {
            throw new ServiceException("校验参数失败");
        }
        pointService.batchDel(dto.getIds());
        return AjaxResult.success();
    }

    @GetMapping("/delByStation")
    public AjaxResult delByStation(@RequestParam("stationId") String stationId) {

        pointService.delByStation(stationId);
        return AjaxResult.success();
    }

    /**
     * 更改状态
     */
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysPointDto dto) {
        pointService.changeStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 自动匹配字段
     */
    @GetMapping("matchMapField")
    public AjaxResult matchMapField(@RequestParam("id") Long id) {
        pointService.matchMapField(id);

        return AjaxResult.success();
    }

    /**
     * 自动匹配字段
     */
    @GetMapping("matchStandardField")
    public AjaxResult matchStandardField(@RequestParam("id") Long id) {
        pointService.matchStandardField(id);
        return AjaxResult.success();
    }

    @PostMapping("handleExpression")
    public AjaxResult handleExpression() {
        return AjaxResult.success();
    }


    @Log(title = "点表数据", businessType = BusinessType.EXPORT)
    @PostMapping("exportPointData")
    public void exportPointData(HttpServletResponse response, SysPointDto pointDto) {

        List<SysPointVo> list = Lists.newArrayList();
        ExcelUtil<SysPointVo> util = new ExcelUtil<SysPointVo>(SysPointVo.class);
        util.exportExcel(response, list, "点表数据");
    }

}
